<template>
  <div>
    <Menu menuActive="/faq" />
    <layout>
      <template v-slot:main>
        <el-row :gutter="20">
          <el-col :lg="18" style="margin-bottom: 20px;">
            <div class="question_container height_light_bgcolor">
              <h1>请描述你遇到的问题</h1>
              <div class="markdown_wrapper">
                <editor v-model="contents" :min-height="500" :height="500" @update:input='changeInput' />
              </div>
            </div>
          </el-col>
          <el-col :lg="6">
            <div class="question_sub_container">
              <el-card class="box-card" shadow="never">
                <template #header>
                  <div class="card_header">
                    <span>其他</span>
                  </div>
                </template>
                <div class="card_content">
                  <el-form size="large" label-width="120px" label-position="top" ref="ruleFormRef" :model="ruleForm"
                    :rules="rules">
                    <el-form-item label="填写标题" prop="title">
                      <el-input v-model="ruleForm.title" placeholder="请输入文章标题" />
                    </el-form-item>
                    <el-form-item label="选择频道" prop="channel">
                      <el-select v-model="ruleForm.channelId" placeholder="请选择频道" style="width:100%;">
                        <el-option v-for="item in channelOptions" :key="item.value" :label="item.label"
                          :value="item.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="选择标签" prop="tag">
                      <el-select v-model="ruleForm.tags" multiple :reserve-keyword="false" style="width:100%;"
                        placeholder="请选择标签,最多可选择5个" :multiple-limit="5">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item>
                      <el-button type="primary" style="width: 100%;" :loading="btnLoading"
                        @click="submitForm(ruleFormRef)">提交问题</el-button>
                    </el-form-item>
                  </el-form>
                </div>
              </el-card>
            </div>
          </el-col>
        </el-row>
      </template>
    </layout>
    <Footer />
  </div>
</template>

<script lang="ts" setup>
import Menu from '@/components/Menu/index.vue'
import Layout from '@/components/Layout/index.vue'
import Footer from '@/components/Footer/index.vue'
import Avatar from '@/components/Avatar/index.vue'
import {
  Tickets,
  UserFilled,
  RefreshLeft,
  Plus,
  Document,
  User,
  ChatDotSquare,
  View,
  Timer,
  Pointer,
  More,
  Location,
  Link,
  OfficeBuilding
} from '@element-plus/icons-vue'
import { onMounted, ref, getCurrentInstance, toRaw, nextTick, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import editor from '@/components/QuillEditor/index.vue'
import { getTag, getChannel } from '@/api/dictionary'
import { ElMessage, ElMessageBox } from 'element-plus'
import { publishFaq } from '@/api/faq'
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  title: '',
  channelId: '',
  tags: ''
})
const channelOptions = ref<any[]>([])
const contents = ref<string>('')
const rules = reactive<FormRules>({
  title: [
    { required: true, message: '标题不能为空', trigger: 'blur' },
    { min: 1, max: 50, message: '标题字数只能在1-50之间', trigger: 'blur' },
  ],
  channelId: [
    { required: true, message: '频道不能为空', trigger: 'blur' },
  ],
  tags: [
    { required: true, message: '标签不能为空', trigger: 'blur' },
  ],
})
const options = ref<any[]>([])
const btnLoading = ref<boolean>(false)
// 获取标签
const getTagData = async () => {
  const { data }: any = await getTag()
  options.value = data.map((item: { name: any; id: any }) => ({ label: item.name, value: item.id }))
}
// 获取频道
const getChannelData = async () => {
  const { data }: any = await getChannel()
  channelOptions.value = data.map((item: { name: any; id: any }) => ({ label: item.name, value: item.id }))
}
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!contents.value) {
    return ElMessage({
      message: '问题内容不能为空',
      type: 'error',
      duration: 2 * 1000
    })
  }
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        btnLoading.value = true
        if (ruleForm.tags) {
          ruleForm.tags = ruleForm.tags.toString()
        }
        const res: any = await publishFaq({ content: contents.value, ...ruleForm })
        if (res) {
          ElMessage({
            message: '发布成功',
            type: 'success',
            duration: 2 * 1000
          })
          location.reload()
        }
      } finally {
        btnLoading.value = false
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
const changeInput = (value: string) => {
  contents.value = value
}
onMounted(() => {
  getTagData()
  getChannelData()
})
</script>

<style scoped lang="scss">
// @import '@/assets/style/reset.scss';
@import './index.scss';
</style>
